package com.caochenlei.greedyAlgorithm;

import java.util.Arrays;

public class AssignCookies {
    public static void main(String[] args) {
        int[] g1 = {1, 2, 3};
        int[] s1 = {1, 1};
        System.out.println(findContentChildren(g1, s1));

        int[] g2 = {1, 2};
        int[] s2 = {1, 2, 3};
        System.out.println(findContentChildren(g2, s2));
    }

    //核心代码
    public static int findContentChildren(int[] children, int[] cookies) {
        Arrays.sort(children);  //从小到大升序排序孩子饥饿度
        Arrays.sort(cookies);   //从小到大升序排序饼干的大小
        int child = 0;          //能吃饱的孩子下标
        int cookie = 0;         //被分配的饼干下标

        while (child < children.length && cookie < cookies.length) {
            if (children[child] <= cookies[cookie++]) {
                child++;
            }
        }

        return child;
    }
}
